package com.tuling.code;



public class BubbleInsertSelectSort {
    public void bubbleSort(int[] arry){
        //冒泡排序
        /**
         *  0 -- n-1
         *  0 -- n-2
         *  0 -- n-3
         */
        for (int i = arry.length -1; i >=0; i--) {
            for (int j = 0; j < i ; j++) {
                if(arry[j] > arry[j+1]){
                    swap(arry,j,j+1);
                }
            }
        }


        for (int i = arry.length -1; i >=0 ; i--) {
            for (int j = 1; j <= i; j++) {
                if(arry[j-1] > arry[j]){
                    swap(arry,i,j);
                }
            }
        }
    }

    public void selectSort(int[] arry){
        //选择排序
        //0 --- n-1;
        //1 --- n-1;
        //2 --- n-1;
        for (int i = 0; i < arry.length -1; i++) {
            int index = i;
            for (int j = i +1; j < arry.length ; j++) {
                if(arry[index] > arry[j]){
                    index = j;
                }
                swap(arry,i,j);
            }
        }
    }
    public void insertSort(int[] arry){
        //插入排序
        //0 --- 0;
        //0 --- 1;
        //0 --- 2;
        for (int i = 1; i < arry.length ; i++) {
            for (int j = i-1; j >= 0 && arry[j] < arry[j + 1] ; j--) {
                swap(arry,j+1,j);
            }
        }
    }

    public void swap(int[] arry,int i,int j){
        int temp = arry[i];
        arry[i] = arry[j];
        arry[j] = temp;
    }
}
